package com.cy.lib.util;

import android.text.TextUtils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by CY on 2015/8/6.
 */
public class StreamUtils {

    public static String toStr(InputStream is, String charsetName) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        byte[] data = new byte[1024];
        int index = -1;
        while ((index = is.read(data)) != -1) {
            baos.write(data, 0, index);
        }
        baos.flush();
        baos.close();
        if (TextUtils.isEmpty(charsetName)) {
            return new String(baos.toByteArray());
        }
        return new String(baos.toByteArray(), charsetName);
    }

    public static String toStr(InputStream is) throws IOException {
        return toStr(is, null);
    }
}
